package com.itown.expo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import webframe.common.helper.ControllerHelper;

import com.itown.expo.controller.base.MyBaseCommandController;
import com.itown.expo.model.Decl;
import com.itown.expo.model.SupervisePlan;

public class DeclModifyController extends MyBaseCommandController {

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command)
			throws RuntimeException {
		// declIds
		Long declId = ControllerHelper.extractId(request);
		Decl decl = getServiceContainer().getCheckBizService().findDeclById(
				declId);

		// opt 操作类型 1=无效单 2=修改备案号 3=更新ciq查验结果 4=提交报检单监管结果 5=修改发货人
		int opt = Integer.parseInt(request.getParameter("opt"));
		if (opt == 1) {
			getServiceContainer().getCheckBizService().updateDeclToUnValid(
					declId, ControllerHelper.getUserBySession());
		} else if (opt == 2) {
			String newbackupCode = getBackupCode(request);
			getServiceContainer().getCheckBizService().updateDeclBackupCode(
					declId, newbackupCode);
			return getView(declId, decl.getType());
		} else if (opt == 5) {
			String newbackupCode = request.getParameter("newbackupCode");
			// Decl decl = getServiceContainer().getCheckBizService()
			// .findDeclById(declId);
			decl.setConsignorCname(newbackupCode);
			getServiceContainer().getCheckBizService().updateDecl(decl);
			return getView(declId, decl.getType());
		} else if (opt == 3) {
			getServiceContainer().getCheckBizService().updateDeclResult(declId);
			return getView(declId, decl.getType());
		} else if (opt == 4) {
			Long result = ControllerHelper.extractId(request, "result");
			String remark = request.getParameter("remark");
			SupervisePlan supervisePlan = new SupervisePlan();
			supervisePlan.setResult(result);
			supervisePlan.setRemark(remark);
			if (decl.getType().equals("B")) {
				getServiceContainer().getCheckBizService()
						.updateMainSupervisePlan(declId, supervisePlan);
				return getView(declId, decl.getType());
			}
		}

		return null;
	}

	private String getBackupCode(HttpServletRequest request) {
		String newbackupCode = request.getParameter("newbackupCode")
				.toUpperCase();
		if (newbackupCode.startsWith(getAppPrefix())) {
			newbackupCode = newbackupCode.substring(getAppPrefix().length());
		}
		newbackupCode = newbackupCode.substring(0, getBackupCodeLength());
		return newbackupCode;
	}

	private ModelAndView getView(Long declId, String type) {
		ModelAndView view = new ModelAndView();
		if (type.equals("A")) {
			view = new ModelAndView(new RedirectView("NormalDeclView.html?id="
					+ declId));
		} else if (type.equals("B")) {
			view = new ModelAndView(new RedirectView("MainDeclView.html?id="
					+ declId));
		}
		return view;
	}
}
